#ifndef JSONRCPMESSAGEPARSER_H
#define JSONRCPMESSAGEPARSER_H

#include "IProtocolHandler.h"
#include <QJsonArray>

/**
 * @brief JSON-RPC message parser implementation
 * @details Validates and parses JSON-RPC messages according to JSON-RPC 2.0 specification
 */
class JsonRpcMessageParser : public IMessageParser
{
public:
    explicit JsonRpcMessageParser();
    ~JsonRpcMessageParser() override = default;

public:// IMessageParser interface implementation
    bool validateMessage(const QJsonObject& message) override;
    QString parseMethodName(const QJsonObject& request) override;
    QJsonObject parseParameters(const QJsonObject& request) override;
    bool isNotification(const QJsonObject& message) override;

private:
    /**
     * @brief Validate JSON-RPC request object.
     * Validates that a request object contains all required fields
     * according to JSON-RPC 2.0 specification: jsonrpc, method, and id
     * @param message - JSON object to validate as a request
     * @return bool - true if valid request, false otherwise
     */
    bool validateRequest(const QJsonObject& message) const;
    
    /**
     * @brief Validate JSON-RPC response object.
     * Validates that a response object contains all required fields
     * according to JSON-RPC 2.0 specification: jsonrpc, result/error, and id
     * @param message - JSON object to validate as a response
     * @return bool - true if valid response, false otherwise
     */
    bool validateResponse(const QJsonObject& message) const;
    
    /**
     * @brief Validate JSON-RPC notification object.
     * Validates that a notification object contains all required fields
     * according to JSON-RPC 2.0 specification: jsonrpc and method
     * @param message - JSON object to validate as a notification
     * @return bool - true if valid notification, false otherwise
     */
    bool validateNotification(const QJsonObject& message) const;
    
    /**
     * @brief Check if JSON object contains required fields.
     * Utility method to verify that a JSON object contains all
     * specified required fields with non-null values
     * @param message - JSON object to check
     * @param requiredFields - list of field names that must be present
     * @return bool - true if all required fields are present, false otherwise
     */
    bool hasRequiredFields(const QJsonObject& message, const QStringList& requiredFields) const;
};

#endif // JSONRCPMESSAGEPARSER_H
